package com.gdth.service.notification;

import com.gdth.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

/** 
 * @author Arvin: 
 * @version 2015年9月7日 上午11:42:06 
 * 类说明 
 */
public class PollingService extends Service{
	public static final String ACTION = "com.ryantang.service.PollingService";
	
	private int count = 0;
	
	private Notification mNotification;
	private NotificationManager mManager;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		initNotifiManager();
	}
	
	/**
	 * Initialize the notification bar configuration
	 */
	private void initNotifiManager() {
		mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		new PollingThread().start();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		System.out.println("Service:onDestroy");
	}

	/**
	 * Polling thread
	 * @Author Arvin
	 * @Create 2013-7-13 上午10:18:34
	 */
	class PollingThread extends Thread {
		@Override
		public void run() {
			count ++;
			//当计数能被5整除时弹出通知
			if (count % 5 == 0) {
				showNotification();
			}
		}
	}
	


	/**
	 * The pop-up Notification
	 */
	private void showNotification() {
		//Navigator to the new activity when click the notification title
		Intent i = new Intent(this, null);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i,
				Intent.FLAG_ACTIVITY_NEW_TASK);
		
		mNotification = new Notification.Builder(this)    
        .setAutoCancel(true)    
        .setContentTitle(getResources().getString(1))    
        .setContentText("You have new message!")    
        .setContentIntent(pendingIntent)    
        .setSmallIcon(R.drawable.drawdownlist_btn_down)    
        .setWhen(System.currentTimeMillis())    
        .build();   

		mManager.notify(0, mNotification);
	}

}
